package com.cskaoyan.service;

import com.cskaoyan.bean.MarketStorage;
import com.cskaoyan.bean.MarketStorageExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketLogMapper;
import com.cskaoyan.mapper.MarketStorageMapper;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author gatsby
 * @since 2022/10/12 09:46
 */
@Service
public class AdminStorageServiceImpl implements AdminStorageService {

    @Autowired
    MarketStorageMapper marketStorageMapper;

    /**
     * 上传管理员图片的业务逻辑
     *
     * @param file
     * @return com.cskaoyan.bean.MarketStorage
     * @author gatsby
     * @since 2022/10/12 19:14
     */
    @Transactional
    @Override
    public MarketStorage uploadFile(MultipartFile file) {
        //文件类型
        String type = file.getContentType();
        //文件原始名字
        String name = file.getOriginalFilename();
        //文件的大小
        int size = (int) file.getSize();
        //获取uuid
        UUID uuid = UUID.randomUUID();
        int index = type.indexOf("/");
        //将uuid与 ".jpg"拼接起来作为文件的唯一标识
        String key = uuid.toString() + "." + type.substring(index + 1);
        String url = "http://localhost:8083/wx/storage/fetch/" + key;
        File destPath = new File("d:\\spring/wx/storage/fetch/", key);
        //将文件保存到磁盘中
        try {
            file.transferTo(destPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //new File("");
        MarketStorage marketStorage = new MarketStorage();
        marketStorage.setKey(key);
        marketStorage.setName(name);
        marketStorage.setSize(size);
        marketStorage.setType(type);
        marketStorage.setUrl(url);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String addTime = sdf.format(new Date());
        Date time = null;
        try {
            time = sdf.parse(addTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        marketStorage.setAddTime(time);
        marketStorage.setUpdateTime(time);
        //先插入
        marketStorageMapper.insertSelective(marketStorage);
        //marketStorageMapper.insert(marketStorage);
        //再查询
        MarketStorage marketStorage1 = marketStorageMapper.selectByPrimaryKey(marketStorage.getId());

        marketStorage.setAddTime(marketStorage1.getAddTime());
        marketStorage.setUpdateTime(marketStorage1.getUpdateTime());

        return marketStorage;
    }

    /**
     * 分页查询管理员对象的业务逻辑
     *
     * @param basePageInfo
     * @return java.util.List<com.cskaoyan.bean.MarketStorage>
     * @author gatsby
     * @since 2022/10/12 19:15
     */
    @Override
    public CommonData<MarketStorage> queryStorageLimit(BasePageInfo basePageInfo, String key, String name) {

        //开启分页
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());

        MarketStorageExample example = new MarketStorageExample();
        example.setDistinct(true);
        //添加排序条件
        example.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());

        MarketStorageExample.Criteria criteria = example.createCriteria();
        //判断模糊查询的两个条件是否为空
        if (!StringUtil.isEmpty(key)) {
            criteria.andKeyLike("%" + key + "%");
        }
        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        //查询出deleted为false的数据
        criteria.andDeletedEqualTo(false);
        List<MarketStorage> marketStorageList = marketStorageMapper.selectByExample(example);

        //获取分页相关信息
        PageInfo<MarketStorage> pageInfo = new PageInfo<>(marketStorageList);
        int pages = pageInfo.getPages();
        int total = (int) pageInfo.getTotal();

        CommonData<MarketStorage> commonData = new CommonData<>();
        commonData.setList(marketStorageList);
        commonData.setLimit(basePageInfo.getLimit());
        commonData.setTotal(total);
        commonData.setPage(basePageInfo.getPage());
        commonData.setPages(pages);


        return commonData;
    }

    /**
     * 修改对象name的业务逻辑
     *
     * @param marketStorage
     * @return com.cskaoyan.bean.MarketStorage
     * @author gatsby
     * @since 2022/10/12 20:06
     */
    @Override
    public MarketStorage updateName(MarketStorage marketStorage) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String addTime = sdf.format(new Date());
        Date time = null;
        try {
            time = sdf.parse(addTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //修改更新的时间
        marketStorage.setUpdateTime(time);

        marketStorageMapper.updateByPrimaryKeySelective(marketStorage);

        return marketStorage;
    }

    /**
     * 删除对象的业务逻辑(逻辑删除）
     *
     * @param marketStorage
     * @return void
     * @author gatsby
     * @since 2022/10/12 20:41
     */
    @Override
    public void delete(MarketStorage marketStorage) {
        marketStorage.setDeleted(true);
        marketStorageMapper.updateByPrimaryKeySelective(marketStorage);
    }
}
